package com.cisc.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionManager;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;

/**
 * @Description: XXX
 * @Author: daizh
 * @Time: 2021/6/20 13:33
 */

@MapperScan(basePackages = {"com.cisc.business.product.dao"}, sqlSessionTemplateRef = "productSqlSessionTemplate")
@Configuration
public class ProductDatasourceConfig {

    private final MybatisProperties properties;

    private final String TYPE_ALIASES_PACKAGE = "com.cisc.business.product.dto";
    private final String MAPPER_PATH = "classpath*:mappers/**/*.xml";


    public ProductDatasourceConfig(MybatisProperties properties) {
        this.properties = properties;
    }

    @Bean("productDb")
    @ConfigurationProperties(prefix = "spring.datasource.druid.productdb")
    public DataSource dataSource() {
        return new DruidDataSource();
    }

    @Bean("productSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean(@Qualifier("productDb") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        factory.setDataSource(dataSource);
        //SpringBootVFS 是类扫描器
        //打包成jar时，setTypeAliasesPackage(“xxx”)找不到类的问题。MyBatis通过VFS来扫描，
        //在Spring Boot中由于是嵌套Jar，导致Mybatis默认的VFS实现DefaultVFS无法扫描嵌套Jar中的类，需要改成SpringBootVFS扫描。
        factory.setVfs(SpringBootVFS.class);

        if (this.properties.getConfigurationProperties() != null) {
            factory.setConfigurationProperties(this.properties.getConfigurationProperties());
        }
        if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
            factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
        } else {
            // 配置类别名
            factory.setTypeAliasesPackage(TYPE_ALIASES_PACKAGE);
        }
        if (this.properties.getTypeAliasesSuperType() != null) {
            factory.setTypeAliasesSuperType(this.properties.getTypeAliasesSuperType());
        }
        if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
            factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
        }
        if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
            factory.setMapperLocations(this.properties.resolveMapperLocations());
        } else {
            // 配置扫描的xxxMapper.xml
            ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            factory.setMapperLocations(resolver.getResources(MAPPER_PATH));
        }
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        factory.setConfiguration(configuration);
        return factory.getObject();
    }

    @Bean("productTxManager")
    public TransactionManager transactionManager(@Qualifier("productDb") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "productSqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("productSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
